package com.fourfaith.iot.beidou.utils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/**
 * 基础通用函数
 * 
 * @author zhangqiushui
 *
 */
public final class BaseCommonFunc {
	private static Random m_random = new Random();
	private static String m_randomNumberBase = "0123456789";
	private static String m_randomHexBase = "0123456789ABCDEF";
	private static String m_randomAlphamericBase = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private static char[] m_hexArray = "0123456789ABCDEF".toCharArray();

	/**
	 * 获取当前时间距纪元所经历的秒数
	 * 
	 * @return
	 */
	public static int getNowTime() {
		int iNowTime = (int) (System.currentTimeMillis() / 1000);
		return iNowTime;
	}

	/**
	 * 获取当前时间距纪元所经历的毫秒数
	 * 
	 * @return
	 */
	public static long getNowTimeMS() {
		return System.currentTimeMillis();
	}

	/**
	 * 获取当前时间的字符串描述,yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public static String getNowTimeString() {
		DateTime dt = new DateTime();
		return dt.toString("yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 获取当前日期的字符串描述
	 * 
	 * @return
	 */
	public static String getNowDateString() {
		DateTime dt = new DateTime();
		return dt.toString("yyyyMMdd");
	}

	/**
	 * 获取当前年月的字符串描述
	 * 
	 * @return
	 */
	public static String getNowYMString() {
		DateTime dt = new DateTime();
		return dt.toString("yyyyMM");
	}

	/**
	 * 获取指定时间点的字符串描述
	 * 
	 * @param _iTimeS，距纪元所经历的秒数
	 * @return
	 */
	public static String getTimeString(int _iTimeS) {
		DateTime dt = new DateTime(_iTimeS * 1000L);
		return dt.toString("yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 获取指定时间点的字符串描述
	 * 
	 * @param lTimeMS，距纪元所经历的毫秒数
	 * @return
	 */
	public static String getTimeString(long _lTimeMS) {
		DateTime dt = new DateTime(_lTimeMS);
		return dt.toString("yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 获取当前年份
	 * 
	 * @return
	 */
	public static int getNowYear() {
		DateTime dt = new DateTime();
		return dt.getYear();
	}

	/**
	 * 获取当前月份,1-12
	 * 
	 * @return
	 */
	public static int getNowMonth() {
		DateTime dt = new DateTime();
		return dt.getMonthOfYear();
	}

	/**
	 * 获取当前日期
	 * 
	 * @return
	 */
	public static int getNowDay() {
		DateTime dt = new DateTime();
		return dt.getDayOfMonth();
	}

	/**
	 * 获取当前时间，24小时制
	 * 
	 * @return
	 */
	public static int getNowHour() {
		DateTime dt = new DateTime();
		return dt.getHourOfDay();
	}

	/**
	 * 获取当前分钟
	 * 
	 * @return
	 */
	public static int getNowMinute() {
		DateTime dt = new DateTime();
		return dt.getMinuteOfHour();
	}

	/**
	 * 获取当前秒数
	 * 
	 * @return
	 */
	public static int getNowSecond() {
		DateTime dt = new DateTime();
		return dt.getSecondOfMinute();
	}

	/**
	 * 根据字符串描述的时间转换成时间戳（秒）
	 * 
	 * @param _sTime
	 * @return
	 */
	public static int getTimestampByString(String _sTime) {
		String sRealTime = _sTime;
		int iDotIndex = _sTime.indexOf('.');
		if (iDotIndex != -1) {
			sRealTime = _sTime.substring(0, iDotIndex);
		}
		DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
		DateTime dt = format.parseDateTime(sRealTime);
		return (int) (dt.getMillis() / 1000);
	}

	/**
	 * 生成人工抓拍的imageId
	 */
	public static String genImageId() {
		long ltime = System.currentTimeMillis();

		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		String sDayTime = df.format(ltime);
		sDayTime = sDayTime.substring(2, sDayTime.length());

		return sDayTime;
	}

	/**
	 * 判断年份是否为闰年 判断闰年的条件， 能被4整除同时不能被100整除，或者能被400整除
	 */
	public static boolean isLeapYear(int _year) {
		boolean bLeapYear = false;
		if (_year % 4 == 0 && _year % 100 != 0) {
			bLeapYear = true;
		} else if (_year % 400 == 0) {
			bLeapYear = true;
		}
		return bLeapYear;
	}

	/**
	 * 产生一个处于[0,99]之间的随机整数
	 * 
	 * @return
	 */
	public static int getRandomInt() {
		return Math.abs(m_random.nextInt(100));
	}

	/**
	 * 产生一个处于[0,Delta]之间的随机整数
	 * 
	 * @param iDelta
	 * @return
	 */
	public static int getRandomInt(int _iDelta) {
		return Math.abs(m_random.nextInt(_iDelta + 1));
	}

	/**
	 * 生成指定位数的的十进制字符串
	 * 
	 * @return
	 */
	public static String getRandomNumber(int _iCount) {
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < _iCount; i++) {
			int iIndex = m_random.nextInt(m_randomNumberBase.length());
			sb.append(m_randomNumberBase.charAt(iIndex));
		}

		return sb.toString();
	}

	/**
	 * 生成指定位数的的16进制字符串
	 * 
	 * @return
	 */
	public static String getRandomHex(int _iCount) {
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < _iCount; i++) {
			int iIndex = m_random.nextInt(m_randomHexBase.length());
			sb.append(m_randomHexBase.charAt(iIndex));
		}

		return sb.toString();
	}

	/**
	 * 生成指定位数的由字母数字构成的随机字符串
	 * 
	 * @return
	 */
	public static String getRandomAlphameric(int _iCount) {
		StringBuilder sb = new StringBuilder();

		for (int i = 0; i < _iCount; i++) {
			int iIndex = m_random.nextInt(m_randomAlphamericBase.length());
			sb.append(m_randomAlphamericBase.charAt(iIndex));
		}

		return sb.toString();
	}

	/**
	 * 将有符号的byte数值转换为0-255之间的int数值
	 * 
	 * @param _by
	 * @return
	 */
	public static int byte2Int(byte _by) {
		int iRet = 0;

		iRet = _by & 0xFF;

		return iRet;
	}

	public static int byte2Int_sl(byte[] b) {
		int intValue = 0;
		for (int i = 0; i < b.length; i++) {
			intValue += (b[i] & 0xFF) << (8 * (3 - i));
		}
		return intValue;
	}

	public static int bytes2Int(byte[] _bys) {
		int iRet = 0;
		int iLen = _bys.length;
		// iRet = _by & 0xFF;
//		for( int i = iLen -1 ,j = 0; i >= 0 ; i-- , j++  )
//		{
//			iRet += (_bys[i] & 0xFF )  << (j * 8);
//		}

		if (_bys[0] >= 0x80 || _bys[0] <= -1) {// 负数
			byte[] bysTemp = new byte[iLen];
			for (int i = 0; i < iLen; i++) {
				bysTemp[i] = (byte) (_bys[i] ^ 0xFF);
			}
			for (int i = iLen - 1, j = 0; i >= 0; i--, j++) {
				iRet += (bysTemp[i] & 0xFF) << (j * 8);
			}
			iRet += 1;
			iRet = -iRet;
		} else {// 正数
			for (int i = iLen - 1, j = 0; i >= 0; i--, j++) {
				iRet += (_bys[i] & 0xFF) << (j * 8);
			}
		}
		return iRet;
	}

	public static float bytes2Float(byte[] _bys) {
		return Float.intBitsToFloat(bytes2Int(_bys));
	}

	public static long bytes2Long(byte[] _bys) {
		long lRet = 0;
		int iLen = _bys.length;
		// iRet = _by & 0xFF;
		for (int i = iLen - 1, j = 0; i >= 0; i--, j++) {
			lRet += (_bys[i] & 0xFF) << (j * 8);
		}

		return lRet;
	}

	public static double bytes2Double(byte[] _bys) {
		return Double.longBitsToDouble(bytes2Long(_bys));
	}

	public static long bytes2FactorValueLong(byte[] _abyFactroText) {
		long lSumValue = 0;

		for (int i = 0; i < _abyFactroText.length; i++) {
			if ((_abyFactroText.length - 1 - i) != 0) {
				lSumValue += BaseCommonFunc.byte2Int(_abyFactroText[i]) * Math.pow(256, (_abyFactroText.length - 1 - i));
			} else {
				lSumValue += BaseCommonFunc.byte2Int(_abyFactroText[i]);
			}

		}
		// BigDecimal bid = new BigDecimal(lSumValue);
		return lSumValue;// bid.longValue();
	}

	/**
	 * 将有符号的short数值转换为0-65535之间的int数值
	 * 
	 * @param _s
	 * @return
	 */
	public static int short2Int(short _s) {
		int iRet = 0;

		iRet = _s & 0xFFFF;

		return iRet;
	}

	/**
	 * 将16进制数值转换为10进制数值
	 * 
	 * @param _iHex
	 * @return
	 */
	public static int hex2Int(int _iHex) {
		return Integer.valueOf(String.valueOf(_iHex), 16);
	}

	/**
	 * 获取32位GUID，中间不带-号
	 * 
	 * @return
	 */
	public static String getGuid() {
		UUID uuid = UUID.randomUUID();
		String sUuid = uuid.toString();
		String s = sUuid.replace("-", "");
		return s;
	}

	/**
	 * 获取一个指定文件夹下的指定数量的文件名列表
	 * 
	 * @param _sFilePath，指定文件夹
	 * @param _iCount，指定数量，如果为0，表示全部
	 * @return
	 */
	public static List<String> getFileNameList(String _sFilePath, int _iCount) {
		List<String> lstFileName = null;
		File file = new File(_sFilePath);
		File[] afile = file.listFiles();
		if (afile == null) {
			return lstFileName;
		}
		int iFileCount = afile.length;
		if (iFileCount > 0) {
			lstFileName = new LinkedList<String>();
			int iRef = 1;
			for (int i = 0; i < iFileCount; i++) {
				if (!afile[i].isDirectory()) {
					lstFileName.add(afile[i].getAbsolutePath());
					if (_iCount > 0) {
						if (++iRef > _iCount) {
							break;
						}
					}
				}
			}
		}
		return lstFileName;
	}

	/**
	 * 删除指定文件
	 * 
	 * @param _sFilePath
	 */
	public static void delFile(String _sFilePath) {
		File file = new File(_sFilePath);
		file.delete();
	}

	/**
	 * bcd格式的字节数组转换为字符串
	 * 
	 * @param _aby
	 * @return
	 */
	public static String bcd2String(byte[] _aby) {
		int iLen = _aby.length;
		StringBuilder sb = new StringBuilder(iLen * 2);
		for (int i = 0; i < iLen; i++) {
			sb.append((byte) ((_aby[i] & 0xf0) >>> 4));
			sb.append((byte) (_aby[i] & 0x0f));
		}
		return sb.toString();
	}

	public static String bcd2StringDeviceAddr(byte[] _aby) {
		StringBuffer sbTmp = new StringBuffer(_aby.length * 2);
		int iLen = _aby.length;
		for (int i = 0; i < iLen; i++) {
			sbTmp.append((byte) ((_aby[i] & 0xf0) >>> 4));
			sbTmp.append((byte) (_aby[i] & 0x0f));
		}
		return sbTmp.toString();
	}

	/**
	 * 将16进制字符串转换为byte字节数组
	 * 
	 * @param _sHex
	 * @return
	 */
	public static byte[] hexString2ByteArray(String _sHex) {
		byte[] abyRaw = _sHex.getBytes();
		int iHexLen = abyRaw.length / 2;
		byte[] abyHex = new byte[iHexLen];

		for (int i = 0; i < iHexLen; i++) {
			int iHigh4 = hexChar2Int(abyRaw[2 * i]) << 4;
			abyHex[i] = (byte) (iHigh4 | hexChar2Int(abyRaw[2 * i + 1]));
		}

		return abyHex;
	}

	public static int hexString2Int(String _sHex) {
		int iRet = 0;

		iRet = Integer.parseInt(_sHex, 16);

		return iRet;
	}

	/**
	 * 将16进制数值转换为10进制
	 * 
	 * @param _byHex
	 * @return
	 */
	public static int hexChar2Int(byte _byHex) {
		if (_byHex < m_hexArray[10]) {
			return _byHex - m_hexArray[0];
		} else {
			return _byHex - m_hexArray[10] + 10;
		}
	}

	/**
	 * 将字节数组转换为字符串
	 * 
	 * @param _aby
	 * @return
	 */
	public static String byteArray2String(byte[] _aby) {
		StringBuilder sb = new StringBuilder();

		int iLen = _aby.length;
		for (int i = 0; i < iLen; i++) {
			sb.append((char) _aby[i]);
		}

		return sb.toString();
	}

	public static String bytes2AsciiString(byte[] _aby) {
		StringBuilder sbAscii = new StringBuilder();

		int iLen = _aby.length;
		for (int i = 0; i < iLen; i++) {
			sbAscii.append((char) _aby[i]);
		}

		return sbAscii.toString();
	}

	public static byte[] AsciiString2bytes(String _str) {
		byte[] byStr = _str.getBytes();
		try {
			byStr = _str.getBytes("US-ASCII");
		} catch (UnsupportedEncodingException e) {
			Arrays.fill(byStr, (byte) 0);
		}
		return byStr;
	}

	public static String bytes2String(byte[] _aby) {
		StringBuilder sb = new StringBuilder();

		int iLen = _aby.length;
		for (int i = 0; i < iLen; i++) {
			sb.append((char) _aby[i]);
		}

		return sb.toString();
	}

	/*
	 * 2进制字符串转10进制int
	 */
	public static int binaryString2Integer(String _binaryString) {

		StringBuffer bstrbuf = new StringBuffer();
		for (int i = _binaryString.length() - 1; i >= 0; i--) {
			bstrbuf.append(_binaryString.charAt(i));
		}
		String bString = bstrbuf.toString();
		// System.out.println("bString="+bString);
		int num = 0;
		for (int i = 0; i < bString.length(); i++) {
			int temp = Integer.parseInt(bString.substring(i, i + 1), 2);
			num = num + (temp) * (int) (Math.pow(2, i));
		}
		return num;
	}

	/**
	 * 2进数组制转16进制字符串
	 */
	public static String bytesToHexString(byte[] _src) {
		final int size = _src.length;
		final char[] c = new char[size * 2];
		for (int i = 0; i < size; i++) {
			final int bint = _src[i];
			c[i * 2] = m_hexArray[(bint & 0xf0) >>> 4];
			c[i * 2 + 1] = m_hexArray[bint & 0x0f];
		}
		return new String(c).toUpperCase();
	}

	/**
	 * 判断字符串中是否有包含字母
	 */
	public static boolean isIncludeCharacter(String _str) {
		boolean flag = false;
		String strCharacter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		for (int i = 0; i < strCharacter.length(); i++) {
			if (_str.indexOf(strCharacter.charAt(i)) > -1) {
				flag = true;
			}
		}
		return flag;
	}

	/*
	 * 全部FF字母
	 */
	public static boolean isAllFFCharacter(String _str) {
		boolean flag = false;
		int size = _str.length();

		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < size; i++) {
			sb.append("F");
		}

		String hexstring = sb.toString();
		if (_str.equals(hexstring)) {
			flag = true;
		}
		return flag;
	}

	public static boolean isOneFFCharacter(String _str) {
		boolean flag = false;
		String strCharacter = "FF";

		if (_str.indexOf(strCharacter) > -1) {
			flag = true;
		}
		return flag;
	}

	/**
	 * 扩展要素，数据无效
	 */
	public static boolean isAllAAcharacter(String _str) {
		boolean flag = false;
		int size = _str.length();
		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < size; i++) {
			sb.append("A");
		}
		String hexstring = sb.toString();

		if (_str.equals(hexstring)) {
			flag = true;
		}
		return flag;
	}

	/**
	 * -1值的判断
	 */
	public static boolean isNegativeOneCharacter(String _str) {
		boolean flag = false;
		int size = _str.length() - 3;
		StringBuffer sb = new StringBuffer();

		sb.append("80");
		for (int i = 0; i < size; i++) {
			sb.append("0");
		}
		sb.append("1");
		String hexstring = sb.toString();

		if (_str.equals(hexstring)) {
			flag = true;
		}
		return flag;
	}

	/**
	 * int 转字节数组
	 */
	public static byte[] int2Bytes(int _value) {
		byte[] src = new byte[2];
		src[0] = (byte) ((_value >> 8) & 0xFF);
		src[1] = (byte) (_value & 0xFF);

		return src;
	}

	public static byte[] int2BytesLen(int _value) {
		_value |= 0x8000;
		return int2Bytes(_value);
	}

	/**
	 * 没有小数的长度
	 */
	public static byte int2ByteFactorLen(int _value) {
		byte byvalue = (byte) ((_value << 3) & 248);
		return byvalue;
	}

	/**
	 * 有小数的长度
	 */
	public static int int2intFactorLen(int _valueI, int _valueDec) {
		int value = (((((_valueI + 1) / 2) << (3)) | (_valueDec & 7)));
		return value;
	}

	public static byte[] int2Bytes_Ascii(int value) {
		byte[] src = new byte[2];
		src[0] = (byte) ((value >> 4) & 0xF);
		src[1] = (byte) (value & 0xF);

		return src;
	}

	public static byte[] int3bytes(int _value) {
		byte[] src = new byte[3];
		src[0] = (byte) ((_value >> 16) & 0xFF);
		src[1] = (byte) ((_value >> 8) & 0xFF);
		src[2] = (byte) (_value & 0xFF);

		return src;
	}

	public static byte[] int4bytes(int _value) {
		byte[] src = new byte[4];
		src[0] = (byte) ((_value >> 24) & 0xFF);
		src[1] = (byte) ((_value >> 16) & 0xFF);
		src[2] = (byte) ((_value >> 8) & 0xFF);
		src[3] = (byte) (_value & 0xFF);

		return src;
	}

	/**
	 * yebinghuai bcd2String方法的反过程
	 */
	public static byte[] string2Bcd(String _asc) {
		int len = _asc.length();
		int mod = len % 2;
		if (mod != 0) {
			_asc = "0" + _asc;
			len = _asc.length();
		}

		byte abt[] = new byte[len];
		if (len >= 2) {
			len = len / 2;
		}
		byte bbt[] = new byte[len];

		abt = _asc.getBytes();
		int j, k;
		for (int p = 0; p < _asc.length() / 2; p++) {
			if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
				j = abt[2 * p] - '0';
			} else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
				j = abt[2 * p] - 'a' + 0x0a;
			} else {
				j = abt[2 * p] - 'A' + 0x0a;
			}

			if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
				k = abt[2 * p + 1] - '0';
			} else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
				k = abt[2 * p + 1] - 'a' + 0x0a;
			} else {
				k = abt[2 * p + 1] - 'A' + 0x0a;
			}

			int a = (j << 4) + k;
			byte b = (byte) a;
			bbt[p] = b;
		}
		abt = null;
		return bbt;
	}

	// int字符串转16进制字符串
	public static String intString2hexString(String _s) {
		String str = "";
		for (int i = 0; i < _s.length(); i++) {
			int ch = (int) _s.charAt(i);
			String s4 = Integer.toHexString(ch);
			str = str + s4;
		}
		return str;
	}

	// 转化十六进制编码为字符串
	public static String hexString2intString(String _s) {
		byte[] baKeyword = new byte[_s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(_s.substring(i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			_s = new String(baKeyword, "utf-8");// UTF-16le:Not
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		baKeyword = null;
		return _s;
	}

	public static String getNowDayString() {
		DateTime dt = new DateTime();
		return dt.toString("yyyy-MM-dd");
	}

	public static String getDayByString(String _sTime) {
		String sRealTime = _sTime;
		int iDotIndex = _sTime.indexOf('.');
		if (iDotIndex != -1) {
			sRealTime = _sTime.substring(0, iDotIndex);
		}
		DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
		DateTime dt = format.parseDateTime(sRealTime);
		return dt.toString("yyyy-MM-dd");
	}

	/**
	 * 从字符串得到时间对象
	 */
	public static DateTime getDateTimeByString(String _sTime) {
		String sRealTime = _sTime;
		int iDotIndex = _sTime.indexOf('.');
		if (iDotIndex != -1) {
			sRealTime = _sTime.substring(0, iDotIndex);
		}
		DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
		DateTime dt = format.parseDateTime(sRealTime);
		return dt;
	}

	// yyyyMMddHHmmss to yyyy-MM-dd HH:mm:ss
	public static String getTimeStringByString(String _sTime) {
		String sYear = _sTime.substring(0, 4);
		String sMonth = _sTime.substring(4, 6);
		String sDay = _sTime.substring(6, 8);
		String sHour = _sTime.substring(8, 10);
		String sMinute = _sTime.substring(10, 12);
		String sSecond = _sTime.substring(12, 14);
		return sYear + "-" + sMonth + "-" + sDay + " " + sHour + ":" + sMinute + ":" + sSecond;
	}

	/**
	 * 从时间对象获取年
	 */
	public static int getYearFromDateTime(DateTime _dt) {
		return _dt.getYear();
	}

	/**
	 * 从时间对象获取月
	 */
	public static int getMonthFromDateTime(DateTime _dt) {
		return _dt.getMonthOfYear();
	}

	/**
	 * 从时间对象获取日
	 */
	public static int getDayFromDateTime(DateTime _dt) {
		return _dt.getDayOfMonth();
	}

	/**
	 * 从时间对象获取小时
	 */
	public static int getHourFromDateTime(DateTime _dt) {
		return _dt.getHourOfDay();
	}

	/**
	 * 从时间对象获取分钟
	 */
	public static int getMinuteFromDateTime(DateTime _dt) {
		return _dt.getMinuteOfHour();
	}

	/**
	 * 从时间对象获取秒数
	 */
	public static int getSecondFromDateTime(DateTime _dt) {
		return _dt.getSecondOfMinute();
	}

	/**
	 * 生成系统当前时间和6位随机数字
	 */
	public static String getRandomNumAndTime() {
		StringBuffer sb = new StringBuffer();

		long lTime = System.currentTimeMillis();
		String sTime = String.valueOf(lTime);

		String sRandomNum = getRandomNumber(6);
		sb.append(sTime);
		sb.append(sRandomNum);

		String sRandomNumTime = sb.toString();

		return sRandomNumTime;
	}

	public static int byte2IntAscii(byte _by) {
		int iRet = 0;

		iRet = _by & 0xF;

		return iRet;
	}

	public static short byte2Short(byte _by) {
		short sRet = 0;

		sRet = (short) (_by & 0xFF);

		return sRet;
	}

	/**
	 * 将ByteBuffer转换成String
	 * 
	 * @param _buf
	 * @return
	 */
	public static String getStringFromBuffer(final ByteBuffer _buf) {
		String str = null;

		try {
			CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
			str = decoder.decode(_buf.duplicate()).toString();
		} catch (CharacterCodingException e) {
			e.printStackTrace();
		}

		return str;
	}

	public static String getStringFromBytes(byte[] _bby) {
		String str = null;
		ByteBuffer bbuf = ByteBuffer.allocate(_bby.length);
		bbuf.put(_bby);
		bbuf.flip();
		try {
			CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
			str = decoder.decode(bbuf.duplicate()).toString();
		} catch (CharacterCodingException e) {
			e.printStackTrace();
		}

		return str;
	}

	/**
	 * 将ByteBuffer转换为16进制表示的字符串
	 * 
	 * @param _buf
	 * @return
	 */
	public static String getHexStringFromBuffer(final ByteBuffer _buf) {
		final int offset = _buf.position();
		final int size = _buf.remaining();
		final char[] c = new char[size * 2];
		for (int i = 0; i < size; i++) {
			final int bint = _buf.get(i + offset);
			c[i * 2] = m_hexArray[(bint & 0xf0) >>> 4];
			c[i * 2 + 1] = m_hexArray[bint & 0x0f];
		}
		return new String(c);
	}

	public static String getBcd2tringFromByte(byte _by) {
		StringBuffer sbTmp = new StringBuffer(2);

		sbTmp.append((byte) ((_by & 0xf0) >>> 4));
		sbTmp.append((byte) (_by & 0x0f));

		return sbTmp.toString();
	}

	public static byte[] hexString2Byte(String _s) {
		byte[] raw = _s.getBytes();
		byte[] hex = new byte[raw.length / 2];

		for (int i = 0; i < hex.length; i++) {
			int leftBit4 = hexChar2Int(raw[2 * i]) << 4;
			hex[i] = (byte) (leftBit4 | hexChar2Int(raw[2 * i + 1]));
		}
		return hex;
	}

	public static byte[] hexString2Bytes(String _value) {
		byte[] ret = null;
		try {
			int len = _value.length() / 2;
			ret = new byte[len];
			for (int i = 0; i < len; i++) {
				ret[i] = (byte) Integer.parseInt(_value.substring(i * 2, i * 2 + 2), 16);
			}
		} catch (Exception e) {
		}
		return ret;
	}

	public static String getSeeTime(String _sSeeTime) {
		String sSeeTime = "";
		if (_sSeeTime.length() == 10) {
			sSeeTime = "20" + _sSeeTime;
		} else {
			sSeeTime = _sSeeTime;
		}

		String strtime = "";
		if (sSeeTime.length() == 12) {
			// 组成xxx-xx-xx xx:xx形式以便入库
			strtime = sSeeTime.substring(0, 4) + "-" + sSeeTime.substring(4, 6) + "-" + sSeeTime.substring(6, 8) + " " + sSeeTime.substring(8, 10) + ":" + sSeeTime.substring(10, 12) + ":" + "00";

		} else if (sSeeTime.length() == 14) {
			// 组成xxxx-xx-xx xx:xx:xx形式以便入库
			strtime = sSeeTime.substring(0, 4) + "-" + sSeeTime.substring(4, 6) + "-" + sSeeTime.substring(6, 8) + " " + sSeeTime.substring(8, 10) + ":" + sSeeTime.substring(10, 12) + ":"
					+ sSeeTime.substring(12, 14);

		} else {
			strtime = sSeeTime;
		}
		return strtime;
	}

	public static byte exorCheck(byte[] _bySrc) {
		byte x = _bySrc[0];
		for (int i = 1; i < _bySrc.length; i++) {
			x = (byte) (x ^ _bySrc[i]);
		}
		return x;
	}

	// 对调数组
	public static byte[] reverseBytes(byte[] _bySrc) {

		for (int i = 0; i < _bySrc.length / 2; i++) {
			// 交换在位置array.length-i和index（）上的两个数
			byte temp = _bySrc[_bySrc.length - 1 - i];
			_bySrc[_bySrc.length - 1 - i] = _bySrc[i];
			_bySrc[i] = temp;
		}
		return _bySrc;
	}

	public static String get3float(String str) {
		try {
			String[] arr = str.split("\\.");
			if (arr != null && arr.length > 1 && arr[1] != null) {
				return arr[0] + "." + arr[1].substring(0, 3);
			} else {
				return str;
			}
		} catch (Exception e) {
			return "";
		}

	}

	// 3字节负数处理(减1，取反)的结果
	public static String negative_number_3byte(int _iSrc) {
		if (_iSrc > 0x7FFFFF) {
			_iSrc = _iSrc & 0xFFFFFF;
			int iSrc_minus = (_iSrc - 1) & 0xFFFFFF;
			int iSrc_negative = (~iSrc_minus) & 0xFFFFFF;
			return "-" + iSrc_negative;
		} else {
			return _iSrc + "";
		}
	}

	public static String dateTimeToString(Date _date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sDateTime = sdf.format(_date);
		System.out.println(sDateTime);
		return sDateTime;
	}

	// 十六进制字符串
	public static boolean stringIsHexNumber(String _str) {
		_str = _str.trim();
		boolean flag = false;
		int iLen = _str.length();
		for (int i = 0; i < iLen; i++) {
			char cc = _str.charAt(i);
			if (cc == '0' || cc == '1' || cc == '2' || cc == '3' || cc == '4' || cc == '5' || cc == '6' || cc == '7' || cc == '8' || cc == '9' || cc == 'A' || cc == 'B' || cc == 'C' || cc == 'D'
					|| cc == 'E' || cc == 'F' || cc == 'a' || cc == 'b' || cc == 'c' || cc == 'c' || cc == 'd' || cc == 'e' || cc == 'f') {
				flag = true;
			} else {
				return false;
			}
		}
		return flag;
	}

	/*
	 * public static String bytes2FactorValueIntString(byte[] _abyFactroText) { long
	 * lSumValue = 0;
	 * 
	 * for(int i = 0 ; i < _abyFactroText.length ; i++ ) { if(
	 * (_abyFactroText.length - 1 - i)!= 0 ) { lSumValue +=
	 * BaseCommonFunc.byte2Int(_abyFactroText[i]) * Math.pow(256,
	 * (_abyFactroText.length - 1 - i) ) ; } else { lSumValue +=
	 * BaseCommonFunc.byte2Int( _abyFactroText[i] ); }
	 * 
	 * } BigDecimal bid = new BigDecimal(lSumValue);
	 * 
	 * if(_abyFactroText.length == 4 ) { return bid.intValue() + ""; } else
	 * if(_abyFactroText.length == 2 ) { return bid.shortValue() + ""; } else
	 * if(_abyFactroText.length == 3 ) { return negative_number_3byte(
	 * bid.intValue()) + ""; } else if(_abyFactroText.length == 1 ) { return (
	 * bid.shortValue() & 0xFF ) + ""; } else { return 0 + ""; }
	 * 
	 * }
	 */

//    public static void main(String[] args)
//	{
////		int iTemp = 16774216;
////		String sTemp = negative_number_3byte(iTemp);
////		System.out.println(sTemp);
////		boolean ishex = stringIsHexNumber("D2");
////		System.out.println("ishex= " + ishex);
//    	
//    	byte[] bysXvalue = {(byte)(0xFF), (byte)(0xFF), (byte)(0x2B), (byte)(0x61)};
//    	int iXvalue = BaseCommonFunc.bytes2Int(bysXvalue);
//    	System.out.println("iXvalue==" + iXvalue );
//	}	

}
